/**
 * Bakalarska prace - Inteligentni budik
 * @package sleepCatcher
 * @file FormAlarmSet.cpp
 * @author Jan Pesava - xpesav00
 * @email xpesav00@stud.fit.vutbr.cz
 * @date 24. 12. 2011
 */

#include "forms/FormAlarmSet.h"
#include "models/SCHelpers.h"
#include "SCFormManager.h"
#include "SCTranslator.h"
#include "SCDatabase.h"

FormAlarmSet::FormAlarmSet()
{
}


FormAlarmSet::~FormAlarmSet()
{
	if(this->pAlarm != NULL)
		this->CloseAndGoBack();

	if(this->pTimePicker != NULL)
		delete this->pTimePicker;
}

bool
FormAlarmSet::Initialize()
{
	Construct(L"IDF_ALARM_SET");
	return true;
}

result
FormAlarmSet::OnInitializing(void)
{
	result r = E_SUCCESS;

	//bind fields
	this->pScrollPanel = static_cast<ScrollPanel*>(GetControl("IDC_SCROLLPANEL"));
	this->pTime = static_cast<EditField *>(pScrollPanel->GetControl("IDC_EDITFIELD_TIME"));
	this->pRepeat = static_cast<EditField *>(pScrollPanel->GetControl("IDC_EDITFIELD_REPEAT"));
	this->pName = static_cast<EditField *>(pScrollPanel->GetControl("IDC_EDITFIELD_NAME"));
	this->pAwakeSound = static_cast<EditField *>(pScrollPanel->GetControl("IDC_EDITFIELD_AWAKE_SOUND"));
	this->pSoundLevel = static_cast<Slider *>(pScrollPanel->GetControl("IDC_SLIDER_SOUND_LEVEL"));
	this->pAwakeType = static_cast<EditField *>(pScrollPanel->GetControl("IDC_EDITFIELD_AWAKE_TYPE"));

	//language
	this->SetTitleText(SCTranslator::GetText("IDS_ALARMSSET_TITLE_ADD"));
	this->pTime->SetTitleText(SCTranslator::GetText("IDS_ALARMSSET_TIME"));
	this->pRepeat->SetTitleText(SCTranslator::GetText("IDS_ALARMSSET_REPEAT"));
	this->pName->SetTitleText(SCTranslator::GetText("IDS_ALARMSSET_NAME"));
	this->pAwakeType->SetTitleText(SCTranslator::GetText("IDS_ALARMSSET_TYPE"));
	this->pAwakeSound->SetTitleText(SCTranslator::GetText("IDS_ALARMSSET_SOUND"));

	//Listeners
	this->pTime->AddTouchEventListener(*this);
	this->pRepeat->AddTouchEventListener(*this);
	this->pName->AddTouchEventListener(*this);
	this->pAwakeSound->AddTouchEventListener(*this);
	this->pAwakeType->AddTouchEventListener(*this);

	//focus???
	this->pRepeat->SetViewModeEnabled(true);
	this->pName->SetViewModeEnabled(true);
	this->pAwakeSound->SetViewModeEnabled(true);
	this->pAwakeType->SetViewModeEnabled(true);
	this->pTime->SetViewModeEnabled(true);

	//set Footer
	Footer* pFooter = this->GetFooter();

	FooterItem *pItemSave = new FooterItem();
	pItemSave->Construct(IDA_ALARM_SET);
	pItemSave->SetText(SCTranslator::GetText("IDS_ALARMS_SAVE"));

	FooterItem *pItemBack = new FooterItem();
	pItemBack->Construct(IDA_ALARM_BACK);
	pItemBack->SetText(SCTranslator::GetText("IDS_ALARMS_BACK"));

	pFooter->SetStyle(FOOTER_STYLE_BUTTON_TEXT);
	pFooter->AddItem(*pItemSave);
	pFooter->AddItem(*pItemBack);
	pFooter->AddActionEventListener(*this);

	// Set TimePicker
	this->pTimePicker = new TimePicker();
	this->pTimePicker->Construct(SCTranslator::GetText("IDS_ALARMSTIME_TITLE"));
	this->pTimePicker->Set24HourNotationEnabled(true);
	this->pTimePicker->AddTimeChangeEventListener(*this);

	delete pItemBack;
	delete pItemSave;

	return r;
}

result
FormAlarmSet::OnTerminating(void)
{
	result r = E_SUCCESS;
	return r;
}

void
FormAlarmSet::OnActionPerformed(const Osp::Ui::Control& source, int actionId)
{
	switch (actionId)
	{
		case IDA_ALARM_SET:
			this->SetAlarmAndSave();
			SCFormManager::SwitchToForm(SCFormManager::IDF_ALARM, false, null, SCFormManager::NO_SLIDE);
			break;
		case IDA_ALARM_BACK:
			this->CloseAndGoBack();
			SCFormManager::SwitchToForm(SCFormManager::IDF_ALARM, false, null, SCFormManager::NO_SLIDE);
			break;
		default:
			break;
	}
}

void
FormAlarmSet::OnTouchDoublePressed (const Osp::Ui::Control &source, const Osp::Graphics::Point &currentPosition, const Osp::Ui::TouchEventInfo &touchInfo)
{
	 // Not implemented
}

void
FormAlarmSet::OnTouchFocusIn (const Osp::Ui::Control &source, const Osp::Graphics::Point &currentPosition, const Osp::Ui::TouchEventInfo &touchInfo)
{
	 // Not implemented
}

void
FormAlarmSet::OnTouchFocusOut (const Osp::Ui::Control &source, const Osp::Graphics::Point &currentPosition, const Osp::Ui::TouchEventInfo &touchInfo)
{
	 // Not implemented
}

void
FormAlarmSet::OnTouchLongPressed (const Osp::Ui::Control &source, const Osp::Graphics::Point &currentPosition, const Osp::Ui::TouchEventInfo &touchInfo)
{
	 // Not implemented
}

void
FormAlarmSet::OnTouchMoved (const Osp::Ui::Control &source, const Osp::Graphics::Point &currentPosition, const Osp::Ui::TouchEventInfo &touchInfo)
{
	 // Not implemented
}

void
FormAlarmSet::OnTouchPressed (const Osp::Ui::Control &source, const Osp::Graphics::Point &currentPosition, const Osp::Ui::TouchEventInfo &touchInfo)
{
	 // Not implemented
}

void
FormAlarmSet::OnTouchReleased (const Osp::Ui::Control &source, const Osp::Graphics::Point &currentPosition, const Osp::Ui::TouchEventInfo &touchInfo)
{
	String sourceName = source.GetName();

	if(sourceName == "IDC_EDITFIELD_TIME")
	{
		this->pTimePicker->SetTime(this->pAlarm->GetTime());
		this->pTimePicker->Show();

	} else if(sourceName == "IDC_EDITFIELD_REPEAT")
	{
		SCFormManager::SwitchToForm(SCFormManager::IDF_ALARM_REPEAT, false, null, SCFormManager::SLIDE_LEFT);
	} else if(sourceName == "IDC_EDITFIELD_NAME")
	{
		SCFormManager::SwitchToForm(SCFormManager::IDF_ALARM_NAME, false, null, SCFormManager::NO_SLIDE);
	} else if(sourceName == "IDC_EDITFIELD_AWAKE_TYPE")
	{
		SCFormManager::SwitchToForm(SCFormManager::IDF_ALARM_AWAKE_TYPE, false, null, SCFormManager::SLIDE_LEFT);
	} else if(sourceName == "IDC_EDITFIELD_AWAKE_SOUND")
	{
		SCFormManager::SwitchToForm(SCFormManager::IDF_ALARM_AWAKE_SOUND, false, null, SCFormManager::SLIDE_LEFT);
	} else {
		AppLog("Nedefinovany zdroj.");
	}
}

void
FormAlarmSet::OnSliderBarMoved (Osp::Ui::Controls::Slider &source, int value)
{
	String sourceName = source.GetName();

	if(sourceName == "IDC_SLIDER_SOUND_LEVEL")
	{
		this->pAlarm->SetSoundLevel(value);
	}
}

void
FormAlarmSet::OnTimeChangeCanceled (const Osp::Ui::Control &source)
{
	this->SetFocus();
	this->Show();
}

void
FormAlarmSet::OnTimeChanged (const Osp::Ui::Control &source, int hour, int minute)
{
	this->pAlarm->SetTime(this->pTimePicker->GetTime());
	this->pTime->SetText(SCHelpers::ConvertDateTimeToString(this->pAlarm->GetTime(), String("HH:mm")));
	this->SetFocus();
	this->Show();
}

void
FormAlarmSet::SetAlarmAndSave()
{
	ArrayList* pAlarms = SCAlarm::GetAllAlarms();

	//add to cache
	if(this->pAlarm->GetId() == SCAlarm::NEW_OBJECT)
	{
		pAlarms->Add(*(this->pAlarm));

		//add to cache
		SCDatabase::AddDataModification(SCDatabase::ACTION_ADD, SCFormManager::IDF_ALARM, pAlarms->GetCount() - 1);
		SCDatabase::AddDataModification(SCDatabase::ACTION_ADD, SCFormManager::IDF_ALARM_DELETE, pAlarms->GetCount() - 1);
		SCDatabase::SetReloadAlarms(true);
		SCDatabase::SetReloadAlarmsDelete(true);
	} else {
		//add to cache
		int index;
		pAlarms->IndexOf(*this->pAlarm, index);
		SCDatabase::AddDataModification(SCDatabase::ACTION_MODIFY, SCFormManager::IDF_ALARM, index);
		SCDatabase::AddDataModification(SCDatabase::ACTION_MODIFY, SCFormManager::IDF_ALARM_DELETE, index);
		SCDatabase::SetReloadAlarms(true);
		SCDatabase::SetReloadAlarmsDelete(true);
	}

	this->pAlarm->Save();
}

void
FormAlarmSet::CloseAndGoBack()
{
	if(this->pAlarm != NULL)
	{
		if(this->pAlarm->GetId() == SCAlarm::NEW_OBJECT)
		{
			delete this->pAlarm;
			this->pAlarm = NULL;
		} else {
			this->pAlarm->Load();
		}
	}
}

void
FormAlarmSet::Redraw()
{
	this->Update();
	RequestRedraw(true);
}

void
FormAlarmSet::Update()
{
	if(this->pAlarm != NULL)
	{
		if(this->pAlarm->GetId() == SCAlarm::NEW_OBJECT)
		{
			this->SetTitleText(SCTranslator::GetText("IDS_ALARMSSET_TITLE_ADD"));
		} else {
			this->SetTitleText(SCTranslator::GetText("IDS_ALARMSSET_TITLE_EDIT"));
		}

		this->pTime->SetText(SCHelpers::ConvertDateTimeToString(pAlarm->GetTime(), String("HH:mm")));
		this->pRepeat->SetText(pAlarm->GetAlarmRepeat()->GetRepeatStateString());
		this->pName->SetText(pAlarm->GetName());
		this->pAwakeSound->SetText(pAlarm->GetAwakeSound());;
		this->pAwakeType->SetText(pAlarm->GetAwakeTypeStr());
		this->pSoundLevel->SetValue(pAlarm->GetSoundLevel());

		this->pScrollPanel->ScrollToTop();

		//Set focus
		this->SetFocus();
	}
}

void
FormAlarmSet::SetDefaultValues()
{
	pAlarm->SetDefaultValues();
}

void
FormAlarmSet::SetAlarm(SCAlarm* pAlarm)
{
	this->pAlarm = pAlarm;
}

SCAlarm*
FormAlarmSet::GetAlarm()
{
	return this->pAlarm;
}

int
FormAlarmSet::GetFormId()
{
	return SCFormManager::IDF_ALARM_SET;
}
